﻿using System;
using System.Diagnostics;

public enum DebugModul
{
    Limit, //游戏内关键数据log，发布后不可以被玩家看到，仅内部测试用
    Normal,
    Commander
}

/// <summary>
///     游戏内log管理器 <br />
///     设置预编译宏 OPEN_GOT_DEBUG 后才会参与编译 <br />
///     各模块调试，开启关闭log可调用SetDebugEnabled <br />
///     注意: DebugModul.Limit为游戏内关键log,不可暴露给玩家，发布时会关掉
/// </summary>
public class RtsDebug
{
    private static bool[] s_ModulDebugEnabled; //各个模块的开关, 数组长度对应枚举DebugModul值的数量

    [Conditional("OPEN_GOT_DEBUG")]
    public static void Init()
    {
        var array = Enum.GetValues(typeof(DebugModul));
        if (array.Length == 0)
            return;
        s_ModulDebugEnabled = new bool[array.Length];

 #if UNITY_EDITOR //编辑器下默认全部显示
         for (var i = s_ModulDebugEnabled.Length - 1; i >= 0; i--)
             s_ModulDebugEnabled[i] = true;
 #else
        s_ModulDebugEnabled[(int)DebugModul.Normal] =  true;     
 #endif
    }

    /// <summary>
    ///     各个模块调试时，可用此方法开启关闭log
    /// </summary>
    public static bool IsDebugEnabled(DebugModul debugModul)
    {
        if (s_ModulDebugEnabled == null)
        {
            UnityEngine.Debug.LogError("Debug 未初始化，请先调用Debug.Init方法");
            return false;
        }
        return s_ModulDebugEnabled[(int) debugModul];
    }

    /// <summary>
    ///     各个模块调试时，可用此方法开启关闭log
    /// </summary>
    [Conditional("OPEN_GOT_DEBUG")]
    public static void SetDebugEnabled(DebugModul debugModul, bool enabled)
    {
        if (s_ModulDebugEnabled == null)
        {
            UnityEngine.Debug.LogError("Debug 未初始化，请先调用Debug.Init方法");
            return;
        }

        s_ModulDebugEnabled[(int) debugModul] = enabled;
    }

    [Conditional("OPEN_GOT_DEBUG")]
    public static void Assert(DebugModul debugModul, bool condition)
    {
        if (CheckShow(debugModul))
            UnityEngine.Debug.Assert(condition);
    }

    [Conditional("OPEN_GOT_DEBUG")]
    public static void Assert(DebugModul debugModul, bool condition, object message)
    {
        if (CheckShow(debugModul))
            UnityEngine.Debug.Assert(condition, message);
    }

    [Conditional("OPEN_GOT_DEBUG")]
    public static void Log(DebugModul debugModul, object message)
    {
        if (CheckShow(debugModul))
            UnityEngine.Debug.Log(message);
    }

    [Conditional("OPEN_GOT_DEBUG")]
    public static void LogWarning(DebugModul debugModul, object message)
    {
        if (CheckShow(debugModul))
            UnityEngine.Debug.LogWarning(message);
    }

    [Conditional("OPEN_GOT_DEBUG")]
    public static void LogError(DebugModul debugModul, object message)
    {
        if (CheckShow(debugModul))
            UnityEngine.Debug.LogError(message);
    }

    [Conditional("OPEN_GOT_DEBUG")]
    public static void AssertFormat(DebugModul debugModul, bool condition, string format, params object[] message)
    {
        if (CheckShow(debugModul))
            UnityEngine.Debug.AssertFormat(condition, format, message);
    }

    [Conditional("OPEN_GOT_DEBUG")]
    public static void LogFormat(DebugModul debugModul, string format, params object[] message)
    {
        if (CheckShow(debugModul))
            UnityEngine.Debug.LogFormat(format, message);
    }

    [Conditional("OPEN_GOT_DEBUG")]
    public static void LogWarningFormat(DebugModul debugModul, string format, params object[] message)
    {
        if (CheckShow(debugModul))
            UnityEngine.Debug.LogWarningFormat(format, message);
    }

    [Conditional("OPEN_GOT_DEBUG")]
    public static void LogErrorFormat(DebugModul debugModul, string format, params object[] message)
    {
        if (CheckShow(debugModul))
            UnityEngine.Debug.LogErrorFormat(format, message);
    }

    [Conditional("OPEN_GOT_DEBUG")]
    public static void LogException(DebugModul debugModul, Exception exception)
    {
        if (CheckShow(debugModul))
            UnityEngine.Debug.LogException(exception);
    }

    private static bool CheckShow(DebugModul debugModul)
    {
        if (s_ModulDebugEnabled == null)
        {
            UnityEngine.Debug.LogError("Debug 未初始化，请先调用Debug.Init方法");
            return false;
        }
        return s_ModulDebugEnabled[(int) debugModul];
    }

    #region Normal Debug Log

    [Conditional("OPEN_GOT_DEBUG")]
    public static void Assert(bool condition)
    {
        Assert(DebugModul.Normal, condition);
    }

    [Conditional("OPEN_GOT_DEBUG")]
    public static void Assert(bool condition, object message)
    {
        Assert(DebugModul.Normal, condition, message);
    }

    [Conditional("OPEN_GOT_DEBUG")]
    public static void Log(object message)
    {
        Log(DebugModul.Normal, message);
    }

    [Conditional("OPEN_GOT_DEBUG")]
    public static void LogWarning(object message)
    {
        LogWarning(DebugModul.Normal, message);
    }

    [Conditional("OPEN_GOT_DEBUG")]
    public static void LogError(object message)
    {
        LogError(DebugModul.Normal, message);
    }

    [Conditional("OPEN_GOT_DEBUG")]
    public static void AssertFormat(bool condition, string format, params object[] message)
    {
        AssertFormat(DebugModul.Normal, condition, format, message);
    }

    [Conditional("OPEN_GOT_DEBUG")]
    public static void LogFormat(string format, params object[] message)
    {
        LogFormat(DebugModul.Normal, format, message);
    }

    [Conditional("OPEN_GOT_DEBUG")]
    public static void LogWarningFormat(string format, params object[] message)
    {
        LogWarningFormat(DebugModul.Normal, format, message);
    }

    [Conditional("OPEN_GOT_DEBUG")]
    public static void LogErrorFormat(string format, params object[] message)
    {
        LogErrorFormat(DebugModul.Normal, format, message);
    }

    [Conditional("OPEN_GOT_DEBUG")]
    public static void LogException(Exception exception)
    {
        LogException(DebugModul.Normal, exception);
    }

    #endregion
}